package com.supermarket.manage.provider.ucenter.service;

import com.supermarket.manage.common.dto.ucenter.response.PermissionResponse;
import com.supermarket.manage.common.pojo.Permission;
import com.supermarket.manage.provider.api.ucenter.PermissionService;
import com.supermarket.manage.provider.ucenter.mapper.PermissionMapper;
import com.supermarket.manage.provider.ucenter.mapper.UserMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-01 18:24
 **/
@Service(version = "1.0.0")
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<PermissionResponse> getAllPermission() {
        List<PermissionResponse> result = new ArrayList<>();
        List<Permission> permissionList = permissionMapper.selectList(null);
        permissionList.forEach(p -> {
            PermissionResponse response = new PermissionResponse();
            BeanUtils.copyProperties(p, response);
            result.add(response);
        });
        return result;
    }

    @Override
    public List<PermissionResponse> getPermissionByUserId(Long userId) {
        List<PermissionResponse> allPermission = getAllPermission();
        List<Permission> userPermissions = userMapper.getPermissionListByUserId(userId);
        allPermission.forEach(p -> {
            userPermissions.forEach(up -> {
                if (up.getId().equals(p.getId())) {
                    p.setIsOwn(Boolean.TRUE);
                } else {
                    p.setIsOwn(Boolean.FALSE);
                }
            });
        });
        return allPermission;
    }
}
